//公共JS函数插件
function print_log(data) {
    console.log(data);
}
//ajax get
function ajax_get(url, success_func, params) {
    let success_function = arguments[1] || print_log;

    $.ajax({
        url: url,
        method: 'get',
        beforeSend:function (req) {
            req.setRequestHeader("X-CSRFToken", $.cookie("csrftoken"));
            req.setRequestHeader("Authorization", $.cookie("key"));
            $.showLoading();
        },
        success:function (data) {
            print_log(data);
            $.hideLoading();
            success_function(data, params);
        },
        error:function () {
            $.hideLoading();
            $.alert("网络出错，请稍后重试!");
        }
    });
}

//ajax post
function ajax_post(url, data, success_func, params) {
    let success_function = arguments[2] || print_log;

    $.ajax({
        url: url,
        method: 'post',
        data: data,
        beforeSend:function (req) {
            req.setRequestHeader("X-CSRFToken", $.cookie("csrftoken"));
            $.showLoading();
        },
        success:function (data) {
            print_log(data);
            $.hideLoading();
            success_function(data, params);
        },
        error:function () {
            $.hideLoading();
            $.alert("网络出错，请稍后重试!");
        }
    });
}

//ajax delete
function ajax_delete(url, success_func, params) {
    let success_function = arguments[1] || print_log;

    $.ajax({
        url: url,
        method: 'delete',
        beforeSend:function (req) {
            req.setRequestHeader("X-CSRFToken", $.cookie("csrftoken"));
            $.showLoading();
        },
        success:function (data) {
            print_log(data);
            $.hideLoading();
            success_function(data, params);
        },
        error:function () {
            $.hideLoading();
            $.alert("网络出错，请稍后重试!");
        }
    });
}
//非空校验
function checkStringNull(str) {
  if (str == null || str == "" || str == undefined || str == "null") {
    return true
  }
  return false;
}
//获取验证码倒计时
function requestode() {
  let that = this;
  let agent_tel = $("#agent_tel").val();
  let countdown = 60;
  that.base.ajax_post("/site/get/mobile/msg/code", {"phone": agent_tel}, function (data) {
    if (data.status == 0) {
      /**
       * 请求成功，倒计时开始
       * **/
      settime(obj);
      function settime() {
        if (countdown == 0) {
          $(obj.target).attr("disabled", false).text("获取验证码").removeClass("bg_B1B3B5").addClass("bg_2577EF");
          //按钮失效
          $(obj.target).attr("disabled", false);
          countdown = 60;
          return;
        } else {
          $(obj.target).attr("disabled", true).text(countdown + "s").removeClass("bg_2577EF").addClass("bg_B1B3B5");
          countdown--;
        }
        setTimeout(function () {
          settime(obj)
        }, 1000)
      }
    } else {
      alert(data.detail)
    }
  });
}




//抛出函数
export default {
  checkStringNull,
  ajax_get,
  ajax_post,
  ajax_delete
}
